package com.gouqi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gouqi.common.BaseResponse;
import com.gouqi.common.DeleteRequest;
import com.gouqi.common.ResultUtils;
import com.gouqi.model.dto.question.QuestionAddRequest;
import com.gouqi.model.dto.question.QuestionEditRequest;
import com.gouqi.model.dto.question.QuestionQueryRequest;
import com.gouqi.model.dto.question.QuestionUpdateRequest;
import com.gouqi.model.entity.Question;
import com.gouqi.model.vo.QuestionVO;
import com.gouqi.service.QuestionService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 题目接口
 */
@RestController
@RequestMapping("/question")
@Slf4j
public class QuestionController {

    @Resource
    private QuestionService questionService;

    // region 增删改查

    @ApiOperation("创建题目")
    @PostMapping("/add")
    public BaseResponse<Long> addQuestion(@RequestBody QuestionAddRequest questionAddRequest, HttpServletRequest request) {
        long questionId = questionService.addQuestion(questionAddRequest, request);
        return ResultUtils.success(questionId);
    }

    @ApiOperation("删除题目")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteQuestion(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        questionService.deleteQuestion(deleteRequest, request);
        return ResultUtils.success(true);
    }

    @ApiOperation("更新题目（仅管理员可用）")
    @PostMapping("/update")
    public BaseResponse<Boolean> updateQuestion(@RequestBody QuestionUpdateRequest questionUpdateRequest) {
        questionService.updateQuestion(questionUpdateRequest);
        return ResultUtils.success(true);
    }

    @ApiOperation("根据 id 获取题目（封装类）")
    @GetMapping("/get/vo")
    public BaseResponse<QuestionVO> getQuestionVOById(long id, HttpServletRequest request) {
        QuestionVO questionVO = questionService.getQuestion(id, request);
        return ResultUtils.success(questionVO);
    }

    @ApiOperation("分页获取题目列表（仅管理员可用）")
    @PostMapping("/list/page")
    public BaseResponse<Page<Question>> listQuestionByPage(@RequestBody QuestionQueryRequest questionQueryRequest) {
        Page<Question> questionPage = questionService.getQuestionPage(questionQueryRequest);
        return ResultUtils.success(questionPage);
    }

    @ApiOperation("分页获取题目列表（封装类）")
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<QuestionVO>> listQuestionVOByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
                                                               HttpServletRequest request) {
        Page<QuestionVO> questionVOPage = questionService.getQuestionVOByPage(questionQueryRequest, request);
        return ResultUtils.success(questionVOPage);
    }


    @ApiOperation("分页获取当前登录用户创建的题目列表")
    @PostMapping("/my/list/page/vo")
    public BaseResponse<Page<QuestionVO>> listMyQuestionVOByPage(@RequestBody QuestionQueryRequest questionQueryRequest,
                                                                 HttpServletRequest request) {
        Page<QuestionVO> questionVOPage = questionService.getMyQuestionVOPage(questionQueryRequest, request);
        return ResultUtils.success(questionVOPage);
    }

    @ApiOperation("编辑题目（给用户使用）")
    @PostMapping("/edit")
    public BaseResponse<Boolean> editQuestion(@RequestBody QuestionEditRequest questionEditRequest, HttpServletRequest request) {
        questionService.editQuestion(questionEditRequest, request);
        return ResultUtils.success(true);
    }

    // endregion
}
